//
//  WordCombination.swift
//  AlgorithmOne
//
//  Created by 刘彬彬 on 2020/3/17.
//  Copyright © 2020 LBB. All rights reserved.
// (["hello","world","leetcode"], "welldonehoneyr"))

import Foundation

class WordCombination {
    func countCharacters(_ words: [String], _ chars: String) -> Int {
        var totalLent:Int = 0
        for index in 0..<words.count {
            var lent = 0
            var tempChars = chars
            let str = words[index]
            if str.count > chars.count {
                continue
            }
            for s in str {
                if tempChars.range(of: String(s)) == nil {
                    lent = 0
                    break
                } else {
                    tempChars = removeStringSpecifiedChart(String(s), tempChars)
                    lent += 1
                }
            }
            totalLent += lent
        }
        return totalLent
    }
    
    //删除指定字符串
    func removeStringSpecifiedChart(_ c: String, _ chars: String) -> String {
        var targetString:String = ""
        var a = c
        for s in chars {
            if String(s) != a {
                targetString = targetString + String(s)
            } else {
                a = ""
            }
        }
        return targetString
    }
}
